<?php


namespace App\Service;


use App\Model\Config;

use Hyperf\Utils\Coroutine;
use Hyperf\Utils\Parallel;


class SmsService
{
    public function sendSms($phone,$msg)
    {
        $error =  [
            "-1" => "参数不全",
            "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
            "30" => "密码错误",
            "40" => "账号不存在",
            "41" => "余额不足",
            "42" => "帐户已过期",
            "43" => "IP地址限制",
            "50" => "内容含有敏感词",
            "0"  => "0"
        ];

        $config = Config::where('name','sms')->first();
        $config = $config->config;

        $sendurl = $config->api."sms?u=".$config->user."&p=".md5($config->pass)."&m=".$phone."&c=".urlencode($config->sign . $msg);


        $parallel = new Parallel();

        $parallel->add(function ()use ($sendurl,$error){
            $result = file_get_contents($sendurl);

            if ($error[$result] == "0"){
                return 'ok';
            }

            return $error[$result];
        });

        $results = $parallel->wait();

        return $results[0];
    }
}